<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Unittest Test</title></head>
	<meta title="template local_template.html">
</head>
<body>
<div id="main_title">More Quiz!</div>
<div id='content'>
<div class="notes">
<h4>Minor annoyance / Bug :-(</h4>
<p> Sometimes, seemingly at random,
you may have to click on the Run Unittest button a second time to get it
working properly.
</p>
</div>

<p>For those that don't like doctest-based quizzes <small>[I really can't see who
wouldn't, but that's besides the point]</small>, Crunchy offers instead a
<b>unittest</b>-based quiz mode.  Try to define two functions,
<code title="python">add</code> and <code title="python">sub</code>
that would satisfy the following unittest.</p>
<pre title="unittest">
class MathTest(unittest.TestCase):
    """Test class for mathematical functions"""

    def test_add(self):
        """Tests for the function add"""
        self.assertEquals(add(1, 1), 2)
        self.assertEquals(add(add(-1, 2), 1), 2)
        self.assertEquals(add(40, 2), 42)

    def test_sub(self):
        """Tests for the function sub"""
        self.assertEquals(sub(1, 1), 0)
        self.assertEquals(sub(40, -2), 42)
</pre>
<p>Now, smartie pants, try to solve the Ultimate question...</p>

<pre title="unittest">
class UltimateAnswerTest(unittest.TestCase):
    """Test class for answer objects"""

    def test_answer(self):
        """Tests for UltimateAnswer.__repr__"""

        answer = UltimateAnswer('Life')
        self.assertEquals(repr(answer), '42')

        answer = UltimateAnswer('Universe')
        self.assertEquals(repr(answer), '42')

        answer = UltimateAnswer('Everything')
        self.assertEquals(repr(answer), '42')
</pre>

<h3>For the cheaters among you...</h3>
<p>This should work:</p>
<pre title="python">

def add(a, b):
    """Computes the sum of two entities"""
    return a + b

def sub(a, b):
    """Computes the difference of two entities"""
    return a - b

class UltimateAnswer(object):
    """Class to emulate the behavior of Deep Thought"""
    def __init__(self, ultimate_question):
        self.ultimate_question = ultimate_question

    def __repr__(self):
        return "42"
</pre>

<h3 >Advanced stuff, for tutorial writers</h3>
<p><code title="html">&lt;pre title="unittest [linenumber [=starting_number]]"&gt;...&lt;/pre&gt;
</code> adds a unittest-based quiz to the page; the text
between the <code title="html">&lt;pre&gt;</code> tags is always placed before the textarea and
used as input for the unit test.</p>


</div>
</body>
</html>
